<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<script th:src="@{/static/js/jquery.min.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/js/afquery.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/layui/layui.all.js}" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}" />
		<link rel="stylesheet" type="text/css" th:href="@{/static/css/Common.css}" />
	</head>
	<body>
		<!-- 对话框面板定义 -->
		<div class='assess_dlg_define' style='display:none'>
			<div class='pane' style='padding:10px'>
				<div style='padding:10px'>
					<span>新增问题:</span>
					<input type='text' style='width:100%' placeholder="请输入问题">
				</div>
				<div style='padding:10px'>
					<span>问卷对象:</span>
					 <select class="academyID" >
						<option value="院系主任">院系主任</option>
						<option value="教师">教师</option>
						<option value="学生">学生</option>
					 </select>
				</div>
			</div>
		</div>
		
		<!-- 对话框面板定义 -->
		<div class='assessPublished_dlg_define' style='display:none'>
			<div class='pane' style='padding:10px;overflow-y: scroll;'>
				<div class="layui-form" >
					<table class="layui-table" lay-size="sm">
						<colgroup>
							<col width="50">
							<col>
						</colgroup>
						<thead>
							<tr>
								<th>序号</th>
								<th>问题</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
			</div>
		</div>
		
		<div class="layui-row" id="Main" style="height: 100%;overflow: hidden;">
			<!-- 顶部区 -->
			<div th:replace =" ~{A_Common::Top_Menu}"></div>
			
			<!-- 左侧菜单区 -->
			<div th:replace =" ~{A_Common::Left_Menu}"></div>
			
			<!-- 右侧内容区 -->
			<div th:fragment="Role_Manager" class="layui-col-xs8 layui-col-md10 layui-row" id="right_Content">
				<fieldset class="layui-elem-field site-demo-button" style="padding: 5%;">
					<legend>评价管理</legend>
					  <div class="layui-tab">
					    <ul class="layui-tab-title">
					      <li onclick="M.tab(this)">问卷管理</li>
					      <li onclick="M.tab(this)">发布管理</li>
					      <li onclick="M.tab(this)">统计管理</li>
					    </ul>
					  <div class="layui-tab-content">
					      <div class="layui-tab-item" id="AssessTable">
					      	<table class="layui-table">
					      		<colgroup>
					      			<col width="50">
									<col width="60">
									<col>
									<col width="100">
					      		</colgroup>
					      		<thead>
					      			<tr>
					      				<th><input type="checkbox" onclick="M.SelectAll(this)" /></th>
					      				<th>序号</th>
					      				<th>问题</th>
										<th>调查对象</th>
					      			</tr>
					      		</thead>
					      		<tbody>
					      			<tr th:each="row : ${QuestionList}">
					      				<td><input type="checkbox" name="selectID" /></td>
					      				<td th:idd="${row.id}" th:text="${rowStat.index}+1">1</td>
					      				<td th:text="${row.Question}">阿里巴巴</td>
										<td>
											<select name="Respondents" disabled="disabled" >
												<option th:each ="rows : ${RespondentsList}" th:text="${rows.respondents}"
												 th:value="${rows.respondents}" th:selected="${rows.respondents == row.Respondents}"></option>
											</select>
										</td>
					      			</tr>
					      		</tbody>
					      	</table>
					      	<!-- 								<div class="nothing">
					      							没有查询到有用数据
					      						</div> -->
					      	<div class="layui-row">
					      		<div class="layui-btn-group layui-col-md-offset7">
									<button type="button" class="layui-btn" onclick="openAddAssess()">新增</button>
					      			<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
					      			<button type="button" class="layui-btn" onclick="M.DeleteModel(this)">删除</button>
					      			<button type="button" class="layui-btn" onclick="M.SaveUpdate(this)">保存</button>
					      		</div>
					      	</div>
					      </div>
						  <div class="layui-tab-item" id="PublishTable">
							<div class="layui-form layui-form-item">
							  <label class="layui-form-label">发布对象</label>
							  <div class="layui-input-block">
								<select name="city" lay-verify="required" id="Assess_Role" >
								  <option></option>
								  <option value="院系主任">院系主任</option>
								  <option value="教师">教师</option>
								  <option value="学生">学生</option>
								</select>
							  </div>
							  <div class="layui-form-item">
								<label class="layui-form-label">发布日期</label>
								<div class="layui-input-block">
								  <input type="text" class="layui-input" id="test10" placeholder=" - ">
								</div>
							  </div>
							</div>
							<table class="layui-table">
								<colgroup>
									<col width="60">
									<col>
								</colgroup>
								<thead>
									<tr>
										<th>序号</th>
										<th>问题</th>
									</tr>
								</thead>
								<tbody>
									
								</tbody>
							</table>
							<!-- 								<div class="nothing">
													没有查询到有用数据
												</div> -->
							<div class="layui-row">
								<div class="layui-btn-group layui-col-md-offset10">
									<button type="button" class="layui-btn" onclick="M.Publish()">发布</button>
								</div>
							</div>
						  </div>
						  <div class="layui-tab-item" id="StatisticsTable">
							<table class="layui-table">
								<colgroup>
									<col width="40">
									<col width="60">
									<col>
								</colgroup>
								<thead>
									<tr>
										<th></th>
										<th>序号</th>
										<th>发布时间</th>
										<th>截止时间</th>
										<th>发布对象</th>
										<th></th>
									</tr>
								</thead>
								<tbody>
									<tr th:each="row : ${StatisticsList}">
										<td><input type="checkbox" name="selectID" /></td>
										<td th:text="${rowStat.index}+1" th:iid="${row.id}">1</td>
										<td th:text="${row.publishTime}">2018</td>
										<td th:text="${row.overTime}">2019</td>
										<td th:text="${row.respondents}">学生</td>
										<td th:idd="${row.isEnd}" th:iid="${row.id}" onclick="M.Published(this)">[[${row.isEnd ? '查看结果':'已发布'}]]</td>
									</tr>
								</tbody>
							</table>
							<!-- 								<div class="nothing">
													没有查询到有用数据
												</div> -->
							<div class="layui-row">
								<div class="layui-btn-group layui-col-md-offset8">
									<button type="button" class="layui-btn" onclick="M.selectMerge(this)">合并统计</button>
									<button type="button" class="layui-btn" onclick="M.DeleteStatistics(this)">删除</button>
								</div>
							</div>
						  </div>	
					  </div>
					</div>
				</fieldset>
			</div>
		</div>
	</body>
	<!-- 初始化点击事件 -->
	<script th:replace =" ~{A_Common::Init_Click}"></script>
	<script th:inline="javascript">
		
		// M.Init = function(){
			let ints=self.setInterval("clock()",10000);
			
		// }();
		
		function clock()
		{
		var d=new Date();
		var t=d.toLocaleTimeString();
		//获取当前时间
		let currentTime = d;
		let data = [[${StatisticsList}]];
		let trs = $("#StatisticsTable tbody tr");
		let array = new Array();
		let Listening = false;
		for(let i=0;i<data.length;i++){
			let customTime = data[i].overTime;	//结束时间
			customTime= customTime.replace("-","/");//替换字符，变成标准格式
			customTime= new Date(Date.parse(customTime));
			console.log(customTime);
			if(currentTime < customTime){	//当前时间<截止时间 
				// alert("当前时间小于自定义时间");
				Listening = true;//继续监听
			}else{
				if(! data[i].isEnd){
					array.push(data[i].id);//保存id,修改为已截止
				}
			}
		}
		if(0 < array.length){
			Af.rest([[@{assess_updatePublish.do}]],array,function(data){
				window.location.reload();
			});
		}
		if(! Listening){
			window.clearInterval(ints);
		}
			
		}
		
		
		
		//自定义时间
		// var customTime="2019-03-01 00:00:00";  
		
		
		// console.log(customTime);
		// console.log(currentTime);
		// if(currentTime < customTime){
		// 	alert("当前时间小于自定义时间");
		// }
		
	var QuestionList = [[${QuestionList}]];
	var s = [[${StatisticsList}]];
	var Role = "";
	var PublishTime = "";
	var EndTime = "";
	// var M = {};
	//Demo
	layui.use(['laydate','form'], function(){
	  var form = layui.form;
	  form.render();
	  //监听select
	  form.on('select()', function(data){
	   console.log(data.value); //得到被选中的值
	   Role = data.value;
	   let count = 0;
	   $("#PublishTable .layui-table tbody").html("");
		for(let i=0;i<QuestionList.length;i++){
			if(QuestionList[i].respondents == data.value){
				count = count+1;
				let str = "<tr>"+
					"<td>"+count+"</td>"+
					"<td>"+QuestionList[i].question+"</td>"+
				"</tr>"
				$("#PublishTable .layui-table tbody").append(str);
			}
		}
				

	 });      
	  var laydate = layui.laydate;
	  //日期时间范围
	    laydate.render({
	      elem: '#test10'
	      ,type: 'datetime'
	      ,range: true
		  ,trigger: 'click' // 新增这一行以解决
		  ,change: function(value, date, endDate){
		       // console.log(value); //得到日期生成的值，如：2017-08-18
		       //    console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
		       //    console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
				 PublishTime = date;
				 EndTime = endDate;
		    }
	    });
	});
	function openAddAssess(){
				
				var dlgContent = $('.assess_dlg_define').html(); // 窗口内容
				
				layer.open({
					type: 1,
					title: '添加记录', 
					area: ['420px', 'auto'], 
					content: dlgContent,
					btn:['确定','放弃'] ,
					
					btn1: function(index,layero){
						let req = {};
						req.question = $(".pane input",layero).val().trim();
						req.respondents = $(".pane select option:selected",layero).val().trim();
						req.isPublish = false;
						Af.rest([[@{assess_saveQuestion.do}]],req,function(data){
							layer.open({
								title: "信息",
								content: '成功！',
								offset: 't',
								time: 500,
								success: function(layero, index){
									setTimeout(function(){location.reload();},600);
								  } 
							});
						});
						
						// 关闭对话框
						layer.close(index)	;				
					},
					btn2 : function(index,layero){
						layer.close(index)		
					}
				});	
				
			}
	// 删除问卷表记录
	M.DeleteModel = function(e){
		let tables = $($(e).parent().parent()).prev();
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		if (checkeds.length > 0) {
			let datas = new Array();
			for (let i = 0; i < checkeds.length; i++) {
				let s = $($(checkeds.get(i)).parent()).next().attr("idd");
				
				datas.push(Number(s));
			}
			
			Af.rest([[@{assess_deleteQuestion.do}]],datas,function(data){
				layer.open({
					title: "信息",
					content: '成功！',
					offset: 't',
					time: 500,
					success: function(layero, index){
						setTimeout(function(){location.reload();},600);
					  } 
				});
			});
			
		}
	}
	
	//删除统计表数据
	M.DeleteStatistics = function(e){
		let tables = $($(e).parent().parent()).prev();
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		if (checkeds.length > 0) {
			let datas = new Array();
			for (let i = 0; i < checkeds.length; i++) {
				let s = $($(checkeds.get(i)).parent()).next().attr("iid");
				
				datas.push(Number(s));
			}
			
			Af.rest([[@{assess_deleteStatisticsn.do}]],datas,function(data){
				layer.open({
					title: "信息",
					content: '成功！',
					offset: 't',
					time: 500,
					success: function(layero, index){
						setTimeout(function(){location.reload();},600);
					  } 
				});
			});
			
		}
	}
	
	//修改信息
	var count = 0;
	M.OpenUpdate = function(e) {
	
		let tables = $($(e).parent().parent()).prev();
		let id = $($(e).parent().parent().parent()).attr("id");
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		count = checkeds.length;
		if (checkeds.length > 0) {
			$(checkeds.parent().parent()).css("background-color","khaki");
			$($(checkeds).parent().parent()).attr("contenteditable", true);
			let trs = $(checkeds).parent().parent();
			for (let i = 0; i < checkeds.length; i++) {
				$($($(trs.get(i)).children()).get(0)).attr("contenteditable", false);
				$($($(trs.get(i)).children()).get(1)).attr("contenteditable", false);
				$($(trs.get(i)).find("td select")).prop("disabled",false);
			}
		}else{
			layer.open({
				title: "信息",
				content: '请选择需要修改的数据！',
				offset: 't',
				time: 2000
			});
		}
	}
	
	//保存修改后信息
	M.SaveUpdate = function(e) {
	
		let tables = $($(e).parent().parent()).prev();
		let trs = $(tables).find("tbody tr");
		
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		let datas = new Array();
		if (checkeds.length != count) {
			layer.open({
				title: "信息",
				content: '请检查修改的数据数量！',
				offset: 't',
				time: 2000
			});
		} else {
				for (let i = 0; i < checkeds.length; i++) {
					var req = {};
					req.id = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("idd"));
					req.question = $($($($(checkeds[i]).parent().parent()).children()).get(2)).text().trim();
					req.respondents = $($(checkeds[i]).parent().parent()).find("select option:selected").val().trim();
					datas.push(req);
					}
					Af.rest([[@{assess_updateQuestion.do}]], datas, function(data) {
						layer.open({
							title: "信息",
							content: '成功！',
							offset: 't',
							time: 500,
							success: function(layero, index){
								setTimeout(function(){location.reload();},600);
							  } 
						});
					});
		}
	}
	
	// 发布问卷
	M.Publish = function(){
		if(Role == "" || PublishTime == ""){
			layer.open({
				title: "信息",
				content: '信息不完整！',
				offset: 't',
				time: 2000
			});
			return;
		}
		let trs = $("#PublishTable .layui-table tbody tr");
		let array = new Array();
		for(let i = 0;i<trs.length;i++){
			let req = {};
			req.respondents = Role;
			req.PublishTime = `${PublishTime.year}-${PublishTime.month}-${PublishTime.date} ${PublishTime.hours}:${PublishTime.minutes}:${PublishTime.seconds}`;
			req.OverTime = `${EndTime.year}-${EndTime.month}-${EndTime.date} ${EndTime.hours}:${EndTime.minutes}:${EndTime.seconds}`;
			req.Question = $($(trs).get(i)).children().next().text();
			array.push(req);
		}
		
		let req = {};
		req.respondents = Role;
		req.PublishTime = `${PublishTime.year}-${PublishTime.month}-${PublishTime.date} ${PublishTime.hours}:${PublishTime.minutes}:${PublishTime.seconds}`;
		req.OverTime = `${EndTime.year}-${EndTime.month}-${EndTime.date} ${EndTime.hours}:${EndTime.minutes}:${EndTime.seconds}`;
		req.isEnd = false;
		
		let obj = {};
		obj.statistics = req;
		obj.publish = array;
		Af.rest([[@{assess_savePublish.do}]],obj,function(data){
			layer.open({
				title: "信息",
				content: '成功！',
				offset: 't',
				time: 500,
				success: function(layero, index){
					setTimeout(function(){location.reload();},600);
				  } 
			});
		});
	}
	
	//查看已发布
	M.Published = function(e){
		if($(e).text() == "已发布"){
			let req = {};
			req.id = Number($(e).attr("iid"));
			Af.rest([[@{assess_getThisPublish.do}]],req,function(data){	
				
				layui.use('form', function(){
				    var form = layui.form;
					var dlgContent = $('.assessPublished_dlg_define').html(); // 窗口内容
					layer.open({
						type: 1,
						title: '发布记录', 
						area: ['600px', '400px'], 
						content: dlgContent,
						btn:['确定'] ,
						success: function(layero,index){
							for(let i=0;i<data.length;i++){
								let str = "<tr>"+
									"<td>"+Number(i+1)+"</td>"+
									"<td>"+data[i].question+"</td>"+
								"</tr>";
								$(".pane .layui-table tbody",layero).append(str);
							}
						},
						
						btn1: function(index,layero){

								// 关闭对话框
								layer.close(index);				
						}
					});	
				
				});
			});
		}else{
			let id = Number($(e).attr("iid"));
			let respondents = $(e).prev().text();
			window.location = "viewmore?AS_id="+id+"&respondents="+respondents;
		}
	}
	
	//查看学生、教师、院系主任的合并成绩 
	M.selectMerge = function(e){
		let tables = $($(e).parent().parent()).prev();
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		let student_AS_id = "";
		let teacher_AS_id = "";
		let dean_AS_id = "";
		if(checkeds.length != 3){
			alert("请选择学生、教师、院系主任各一条数据");
			return;
		}else{
			let s = $($(checkeds.get(0)).parent().parent()).find("td:eq(4)").text();
			let ss = $($(checkeds.get(1)).parent().parent()).find("td:eq(4)").text();
			let sss = $($(checkeds.get(2)).parent().parent()).find("td:eq(4)").text();
			
			let s1 = $($(checkeds.get(0)).parent().parent()).find("td:eq(5)").text();
			let ss2 = $($(checkeds.get(1)).parent().parent()).find("td:eq(5)").text();
			let sss3 = $($(checkeds.get(2)).parent().parent()).find("td:eq(5)").text();
			if(s != ss && ss!= sss && sss != s && s1==ss2 && ss2 == sss3 && ss3=="查看结果"){
				for(let i=0;i<3;i++){
					let kkk = $($(checkeds.get(i)).parent().parent()).find("td:eq(4)").text();
					switch($($(checkeds.get(i)).parent().parent()).find("td:eq(4)").text()){
						case "学生":student_AS_id = $($(checkeds.get(i)).parent().parent()).find("td:eq(5)").attr("iid");
						break;
						case "教师":teacher_AS_id = $($(checkeds.get(i)).parent().parent()).find("td:eq(5)").attr("iid");
						break;
						case "院系主任":dean_AS_id = $($(checkeds.get(i)).parent().parent()).find("td:eq(5)").attr("iid");
						break;
					}
				}
			}else{
				alert("信息尚未结算");
			}
		}
		 
	}
	
	var page = sessionStorage.getItem("page");
	M.init = function(){
		$(tab_titles).removeAttr("class");
		$(tab_contents).hide();
		if(Number(page) >= 0){
			$(tab_titles).removeAttr("class");
			$(tab_contents).hide();
			
			switch(Number(page)){
				case 0:
					sessionStorage.setItem("page",0);
					$(".layui-tab-title li:eq(0)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
				case 1:
					sessionStorage.setItem("page",1);
					var s = $(".layui-tab-title li:eq(1)").attr("class","layui-this");
					var m = $(".layui-tab-content .layui-tab-item:eq(1)").show();
				break;
				case 2:
					sessionStorage.setItem("page",2);
					$(".layui-tab-title li:eq(2)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(2)").show();
				break;
				default: 
				       sessionStorage.setItem("page",0);
				       $(".layui-tab-title li:eq(0)").attr("class","layui-this");
				       $(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
			}	
		}else{
			sessionStorage.setItem("page",0);
			$(".layui-tab-title li:eq(0)").attr("class","layui-this");
			$(".layui-tab-content .layui-tab-item:eq(0)").show();
			}
		}();
	</script>
	
</html>
